

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/hello_lixi.gitee.io/img/fluid.png">
  <link rel="icon" href="/hello_lixi.gitee.io/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Mr.LI">
  <meta name="keywords" content="">
  
    <meta name="description" content="docker的安装与配置,docker中安装mysql,redis,nacos,固定容器ip,连接不同网段的容器">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker容器的安装使用">
<meta property="og:url" content="https://xistudycode.gitee.io/hello_lixi.gitee.io/2023/05/11/docker-%E5%AE%B9%E5%99%A8%E7%9A%84%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="Welcome to the technology blog">
<meta property="og:description" content="docker的安装与配置,docker中安装mysql,redis,nacos,固定容器ip,连接不同网段的容器">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194351146.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194437274.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194532313.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194646059.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209135436824.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209135505230.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209105633379.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/%E4%BF%AE%E6%94%B9nacos%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/%E6%9F%A5%E8%AF%A2mysql%E5%9C%B0%E5%9D%802.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209145334682.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209143215300.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209150545109.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209150607093.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209150020679.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/nacos%E9%85%8D%E7%BD%AE%E4%BF%AE%E6%94%B9.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230329145609680.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209160024238.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230413113954453.png">
<meta property="article:published_time" content="2023-05-11T06:39:37.910Z">
<meta property="article:modified_time" content="2023-05-11T06:55:05.037Z">
<meta property="article:author" content="Mr.LI">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194351146.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>Docker容器的安装使用 - Welcome to the technology blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/hello_lixi.gitee.io/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/hello_lixi.gitee.io/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/hello_lixi.gitee.io/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"xistudycode.gitee.io","root":"/hello_lixi.gitee.io/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/hello_lixi.gitee.io/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/hello_lixi.gitee.io/js/utils.js" ></script>
  <script  src="/hello_lixi.gitee.io/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/hello_lixi.gitee.io/">
      <strong>xBlog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/hello_lixi.gitee.io/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Docker容器的安装使用"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        Mr.LI
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-05-11 14:39" pubdate>
          2023年5月11日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          9.9k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          83 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Docker容器的安装使用</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月11日 下午
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h3 id="1-docker安装配置"><a href="#1-docker安装配置" class="headerlink" title="1. docker安装配置"></a>1. docker安装配置</h3><p>下载docker安装程序， win10专业版系统首先需要开启硬件虚拟化及Hyper-V功能，才能进行Docker for Windows软件安装，win10系统，打开控制面板-“应用”-“程序和功能”，开启Hyper-V，按照提示即可，如下图：</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194351146.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230208194351146"></p>
<p>使用wsl2进行安装还要勾选</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194437274.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230208194437274"></p>
<p>配置成功看选项</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194532313.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230208194532313"></p>
<p>2.更改镜像默认位置</p>
<p>docker 默认安装到c 盘 ,c 盘 128固态 太小 蛋疼 , win10 的C盘越来越小 没办法腾出点空间 只有把docker迁移了。最新的版本已经基于windows的 wsl2作为底层架构，通过默认创建两个“发行版”，也就是distro来实现容器化功能，并且内部支持一个简易的kubernetes。<br>可以用wsl命令查看两个默认的distro：</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230208194646059.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230208194646059"></p>
<p>对应到磁盘上面，默认位置 <strong>%LOCALAPPDATA%/Docker/wsl</strong></p>
<ul>
<li>data/ext4.vhdx 对应存储 docker-desktop-data数据，用于存储镜像，会随着使用而不断增加。</li>
<li>distro/ext4.vhdx 对应存储 docker-desktop数据，100多mb，可以暂时不移动。√</li>
</ul>
<p><strong>数据都是存在C盘，为了节省空间，移动数据到其他地方的步骤如下</strong>（最好是在刚安装的时候，打开一次后）：<br>以下命令都是运行在以管理员身份运行的PowerShell</p>
<ol>
<li>停止docker desktop</li>
<li>wsl 移出，注销，再导入distros</li>
</ol>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">wsl --shutdown<br></code></pre></td></tr></table></figure>

<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">wsl --export docker-desktop-data D:\docker\docker-desktop-data.tar<br></code></pre></td></tr></table></figure>

<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">wsl --unregister docker-desktop-data<br></code></pre></td></tr></table></figure>

<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">wsl --import docker-desktop-data D:\docker\data D:\docker\docker-desktop-data.tar --version <span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>

<ol>
<li>重新启动Docker</li>
<li>最后，如果在验证后一切没有问题，则可以删除 E:\ docker-desktop\docker-desktop-data.tar文件</li>
</ol>
<h3 id="2-安装mysql，并挂载本地文件"><a href="#2-安装mysql，并挂载本地文件" class="headerlink" title="2. 安装mysql，并挂载本地文件"></a>2. 安装mysql，并挂载本地文件</h3><p>数据库相关</p>
<h4 id="（1）windows系统使用Docker搭建MySQL服务"><a href="#（1）windows系统使用Docker搭建MySQL服务" class="headerlink" title="（1）windows系统使用Docker搭建MySQL服务"></a>（1）windows系统使用Docker搭建MySQL服务</h4><p>一.docker建立镜像</p>
<p>1.拉取官方镜像（我们这里选择5.7，如果不写后面的版本号则会自动拉取最新版）</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker pull mysql:<span class="hljs-number">5</span>.<span class="hljs-number">7</span>   # 拉取 mysql <span class="hljs-number">5</span>.<span class="hljs-number">7</span><br>docker pull mysql       # 拉取最新版mysql镜像<br></code></pre></td></tr></table></figure>

<p>2.检查是否拉取成功</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker images<br></code></pre></td></tr></table></figure>

<p>3.数据库容器不需要建立目录映射，使用运行</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run -p <span class="hljs-number">3306</span>:<span class="hljs-number">3306</span> --name mysql -e MYSQL_ROOT_PASSWORD=<span class="hljs-number">123456</span> -d mysql:<span class="hljs-number">5</span>.<span class="hljs-number">7</span><br></code></pre></td></tr></table></figure>

<ul>
<li>–name：容器名，此处命名为mysql</li>
<li>-e：配置信息，此处配置mysql的root用户的登陆密码</li>
<li>-p：端口映射，此处映射 主机3306端口 到 容器的3306端口</li>
<li>-d：后台运行容器，保证在退出终端后容器继续运行</li>
</ul>
<p>4.如果要建立目录映射</p>
<p>先在你想要安装的目录下建一个mysql文件夹，在下面创建三个文件夹conf,data,log，再在conf下创建一个文件my.cnf</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209135436824.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209135436824"></p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209135505230.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209135505230"></p>
<p>my.cnf内容如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs cnf">[mysqld]<br>#服务所能处理的请求包的最大大小以及服务所能处理的最大的请求大小(当与大的BLOB字段一起工作时相当必要)，每个连接独立的大小.大小动态增加<br>max_allowed_packet = 128M<br>#MySQL 服务所允许的同时会话数的上限，其中一个连接将被SUPER权限保留作为管理员登录。 即便已经达到了连接数的上限.<br>max_connections=1500<br>symbolic-links=0<br>character-set-server=utf8mb4<br>default-time-zone = &#x27;+8:00&#x27;<br>sql_mode=&#x27;STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION&#x27;<br>skip-grant-tables<br>#不区分大小写<br>lower_case_table_names=1<br><br>#error日志设置<br>log-error=/var/log/mysql/error.log<br><br>#普通日志开启<br>general_log=on<br>general_log_file=/var/log/mysql/general.log<br><br>[client]<br>default-character-set=utf8mb4<br>[mysql]<br>default-character-set=utf8mb4<br></code></pre></td></tr></table></figure>

<p>cmd如下使用命令进行挂载本地文件：</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run -p <span class="hljs-number">3306</span>:<span class="hljs-number">3306</span> --name mysql -v D:\docker\Workspace_docker\mysql57\conf\my.cnf:\etc/mysql\my.cnf -v D:\docker\Workspace_docker\mysql57\logs:/var/log/mysql -v D:\docker\Workspace_docker\mysql57\data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=<span class="hljs-number">123456</span> -d mysql:<span class="hljs-number">5</span>.<span class="hljs-number">7</span><br><br></code></pre></td></tr></table></figure>

<ul>
<li>-v：主机和容器的目录映射关系，”:”前为主机目录，之后为容器目录</li>
<li>当容器被删除则数据和新加的配置全部清空，因此需要对数据持久化以及配置持久化到宿主机上，需要采用docker容器数据卷实，目录的挂载来实现</li>
</ul>
<p>5.检查容器是否正确运行</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker ps<br></code></pre></td></tr></table></figure>

<p>二、连接mysql</p>
<p>1.进入docker本地连接mysql客户端</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs cmd">sudo docker exec -it mysql bash<br>mysql -uroot -p123456<br></code></pre></td></tr></table></figure>

<p>2.使用远程连接软件时要注意一个问题<br>我们在创建容器的时候已经将容器的3306端口和主机的3306端口映射到一起，所以我们应该访问：</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs cmd"><span class="hljs-function">host: 127.0.0.1</span><br><span class="hljs-function"><span class="hljs-title">port</span>: 3306</span><br><span class="hljs-function"><span class="hljs-title">user</span>: <span class="hljs-title">root</span></span><br><span class="hljs-function"><span class="hljs-title">password</span>: 123456</span><br></code></pre></td></tr></table></figure>

<p>如果你的容器运行正常，但是无法访问到MySQL，一般有以下几个可能的原因：</p>
<ul>
<li>防火墙阻拦（linux下）</li>
</ul>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs cmd"># 开放端口：<br>$ systemctl status firewalld<br>$ firewall-<span class="hljs-built_in">cmd</span>  --zone=public --add-port=<span class="hljs-number">3306</span>/tcp -permanent<br>$ firewall-<span class="hljs-built_in">cmd</span>  --reload<br># 关闭防火墙：<br>$ sudo systemctl stop firewalld<br></code></pre></td></tr></table></figure>

<ul>
<li>需要进入docker本地客户端设置远程访问账号</li>
</ul>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs cmd">   $ sudo docker exec -it mysql bash<br>   $ mysql -uroot -p123456<br>   mysql&gt; grant all privileges on *.* to root@&#x27;%&#x27; identified by &quot;password&quot;;<br>   <br>mysql&gt; ALTER USER &#x27;root&#x27;@&#x27;%&#x27; IDENTIFIED WITH mysql_native_password BY &#x27;<span class="hljs-number">123456</span>&#x27;;<br>mysql&gt; ALTER USER &#x27;root&#x27;@&#x27;localhost&#x27; IDENTIFIED WITH mysql_native_password BY &#x27;<span class="hljs-number">123456</span>&#x27;;<br>mysql&gt; SELECT plugin FROM mysql.user WHERE User = &#x27;root&#x27;;<br>mysql&gt; FLUSH PRIVILEGES;<br></code></pre></td></tr></table></figure>

<ul>
<li>原理：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"># mysql使用mysql数据库中的user表来管理权限，修改user表就可以修改权限（只有root账号可以修改）<br><br>mysql&gt; use mysql;<br>Database changed<br><br>mysql&gt; select host,userfrom user;<br>+--------------+------+-------------------------------------------+<br>| host                   | user        |<br>+--------------+------+-------------------------------------------+<br>| localhost              | root        |<br>| <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.1</span>            | root        |<br>+--------------+------+-------------------------------------------+<br><span class="hljs-number">2</span> rows in <span class="hljs-title function_">set</span> <span class="hljs-params">(<span class="hljs-number">0.00</span> sec)</span><br><br>mysql&gt; grant all privileges  on *.* to root@<span class="hljs-string">&#x27;%&#x27;</span> identified by <span class="hljs-string">&quot;password&quot;</span>;<br>Query OK, <span class="hljs-number">0</span> rows <span class="hljs-title function_">affected</span> <span class="hljs-params">(<span class="hljs-number">0.00</span> sec)</span><br><br>mysql&gt; flush privileges;<br>Query OK, <span class="hljs-number">0</span> rows <span class="hljs-title function_">affected</span> <span class="hljs-params">(<span class="hljs-number">0.00</span> sec)</span><br><br>mysql&gt; select host,user from user;<br>+--------------+------+-------------------------------------------+<br>| host                   | user          |<br>+--------------+------+-------------------------------------------+<br>| localhost              | root          |<br>| <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.1</span>            | root          |<br>| %                      | root          |<br>+--------------+------+-------------------------------------------+<br><span class="hljs-number">3</span> rows in <span class="hljs-title function_">set</span> <span class="hljs-params">(<span class="hljs-number">0.00</span> sec)</span><br></code></pre></td></tr></table></figure>


<h4 id="（2）Navicat执行多个-sql文件"><a href="#（2）Navicat执行多个-sql文件" class="headerlink" title="（2）Navicat执行多个.sql文件"></a>（2）Navicat执行多个.sql文件</h4><p>Navicat一次只能加载执行一个.sql 文件，那么有很多.sql文件要执行的时候，就会很浪费时间，可以考虑将多个.sql文件合并成一个文件，这样就可以一次执行结束</p>
<p>在Windows系统的命令行窗口中使用type命令可以实现文本文件合并。这里我是将D盘test/sql目录下的所有.sql文件合并为一个aaa.sql的文件，命令type *.sql &gt;&gt; ..\aaa.sql，会在test目录下生成aaa.sql文件。</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd"><span class="hljs-function">D:\<span class="hljs-title">test</span>\<span class="hljs-title">sql</span>&gt;<span class="hljs-title">type</span> *.<span class="hljs-title">sql</span> &gt;&gt; ..\<span class="hljs-title">aaa.sql</span></span><br></code></pre></td></tr></table></figure>

<p>然后执行这个生成的sql文件即可。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>  <span class="hljs-attr">&quot;builder&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;gc&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>      <span class="hljs-attr">&quot;defaultKeepStorage&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;20GB&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;enabled&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><br>    <span class="hljs-punctuation">&#125;</span><br>  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>  <span class="hljs-attr">&quot;experimental&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span><br>  <span class="hljs-attr">&quot;features&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;buildkit&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><br>  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>  <span class="hljs-attr">&quot;registry-mirrors&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>    <span class="hljs-string">&quot;https://rdttbrhe.mirror.aliyuncs.com&quot;</span><br>  <span class="hljs-punctuation">]</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h3 id="3-docker安装nacos"><a href="#3-docker安装nacos" class="headerlink" title="3. docker安装nacos"></a>3. docker安装nacos</h3><h4 id="（1）安装并配置mysql本地数据库"><a href="#（1）安装并配置mysql本地数据库" class="headerlink" title="（1）安装并配置mysql本地数据库"></a>（1）安装并配置mysql本地数据库</h4><p>1.进入cmd，下载nacos镜像</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker pull nacos/nacos-server<br></code></pre></td></tr></table></figure>

<p>2.查看nacos是否安装成功。查到就安装成功了</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker images<br></code></pre></td></tr></table></figure>

<p>3.启动镜像</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run -d --name nacos -p <span class="hljs-number">8848</span>:<span class="hljs-number">8848</span> -e PREFER_HOST_MODE=hostname -e <span class="hljs-built_in">MODE</span>=standalone nacos/nacos-server<br></code></pre></td></tr></table></figure>

<p>4.查看镜像是否启动成功</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker ps<br></code></pre></td></tr></table></figure>

<p>5.在数据库内新建nacos数据库，并执行nacos数据库的.sql文件，创建数据库表</p>
<p>上述方式是最简便的方式启动，但这样的话有一点小瑕疵，nacos所有元数据都会保存在容器内部。倘若容器迁移则nacos源数据则不复存在，所以通常我们通常会将nacos元数据保存在mysql中，sql执行文件到nacos官网找。</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209105633379.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209105633379"></p>
<p>6.配置本地数据库</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs cmd">#<span class="hljs-number">1</span> 查看docker容器，nacos启动成功<br>docker ps <br>CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                    NAMES<br><span class="hljs-number">8149</span>bca96437        nacos/nacos-server   &quot;bin/docker-startup.…&quot;   <span class="hljs-number">4</span> minutes ago       Up About a minute   <span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>:<span class="hljs-number">8848</span>-&gt;<span class="hljs-number">8848</span>/tcp   nacos<br>#<span class="hljs-number">2</span> 进入容器<br>docker exec -it <span class="hljs-number">8149</span>bca96437 /bin/bash<br>#<span class="hljs-number">3</span> 修改 conf/application.properties 内容如下：<br>vi conf/application.properties<br></code></pre></td></tr></table></figure>

<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/%E4%BF%AE%E6%94%B9nacos%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="修改nacos配置文件"></p>
<p>其中查询mysql在docker内的地址</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker inspect &lt;container id&gt;<br></code></pre></td></tr></table></figure>

<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/%E6%9F%A5%E8%AF%A2mysql%E5%9C%B0%E5%9D%802.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="查询mysql地址2"></p>
<p>按esc退出insert模式，保存退出shift + : + w + q。重启nacos：</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker restart <span class="hljs-number">4516</span>bc87f072<br></code></pre></td></tr></table></figure>

<p>7.访问<a target="_blank" rel="noopener" href="http://localhost:8848/nacos">http://localhost:8848/nacos</a></p>
<h4 id="（2）安装nacos挂载本地文件，配置本地mysql数据库"><a href="#（2）安装nacos挂载本地文件，配置本地mysql数据库" class="headerlink" title="（2）安装nacos挂载本地文件，配置本地mysql数据库"></a>（2）安装nacos挂载本地文件，配置本地mysql数据库</h4><p>nacos版本不能高，要和数据库相匹配，不然将nacos导入的配置持久化到mysql中的时候会报错，因此本机使用mysql5.7，nacos使用1.4.2版本</p>
<p>1.拉取镜像</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker pull nacos/nacos-server:<span class="hljs-number">1</span>.<span class="hljs-number">4</span>.<span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>

<p>2、创建临时容器，主要将容器内部的文件挂载到外部</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run -p <span class="hljs-number">8848</span>:<span class="hljs-number">8848</span> --name nacostest -d nacos/nacos-server:<span class="hljs-number">1</span>.<span class="hljs-number">4</span>.<span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>

<p>3.进入容器查看文件</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker exec -it nacostest bash<br>ls<br></code></pre></td></tr></table></figure>

<p>4.在本地创建文件夹nacos，里面新建conf和logs文件夹</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209145334682.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209145334682"></p>
<p>5.从容器中复制nacos配置文件到本地</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker cp nacostest:\home\nacos\logs D:\docker\Workspace_docker\nacos\<br><br>docker cp nacostest:\home\nacos\conf D:\docker\Workspace_docker\nacos\<br></code></pre></td></tr></table></figure>

<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209143215300.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209143215300"></p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209150545109.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209150545109"></p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209150607093.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209150607093"></p>
<p>6.在数据库内新建nacos数据库，并执行nacos数据库的.sql文件，创建数据库表</p>
<p><a target="_blank" rel="noopener" href="https://github.com/alibaba/nacos/blob/master/config/src/main/resources/META-INF/nacos-db.sql">进入github复制nacos-db.sql文件并执行SQL文件</a></p>
<p>7.删除临时nacostest容器</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker stop nacostest<br>docker rm nacostest<br></code></pre></td></tr></table></figure>
<p>8.进入挂载的本地文件，修改nacos的配置文件nacos\conf\application.properties</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209150020679.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209150020679"></p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/nacos%E9%85%8D%E7%BD%AE%E4%BF%AE%E6%94%B9.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="nacos配置修改"></p>
<p>8.执行挂载命令</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run --name nacos-server --env <span class="hljs-built_in">MODE</span>=standalone -v D:\docker\Workspace_docker\nacos\conf\application.properties:\home\nacos\conf\application.properties -d -p <span class="hljs-number">8848</span>:<span class="hljs-number">8848</span> nacos/nacos-server:<span class="hljs-number">1</span>.<span class="hljs-number">4</span>.<span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>

<p>9.访问<a target="_blank" rel="noopener" href="http://localhost:8848/nacos">http://localhost:8848/nacos</a></p>
<p>如果nacos读取不到mysql的配置信息，可能挂载失败，进入docker里面的nacos配置文件进行修改</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker exec -it nacos-server-<span class="hljs-number">2</span>.<span class="hljs-number">1</span>.<span class="hljs-number">2</span> /bin/bash<br><br><span class="hljs-built_in">cd</span> conf<br><br>vim application.properties<br></code></pre></td></tr></table></figure>

<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230329145609680.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230329145609680"></p>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs ini"><span class="hljs-attr">db.url.0</span>=jdbc:mysql://<span class="hljs-number">172.17</span>.<span class="hljs-number">0.2</span>:<span class="hljs-number">3306</span>/ry-config?characterEncoding=utf8&amp;connectTimeout=<span class="hljs-number">1000</span>&amp;socketTimeout=<span class="hljs-number">3000</span>&amp;autoReconnect=<span class="hljs-literal">true</span><br><br><span class="hljs-attr">db.user</span>=root<br><br><span class="hljs-attr">db.password</span>=password<br><br><br></code></pre></td></tr></table></figure>



<p><strong>注：在docker中，一个镜像可以安装多个容器，暴露不同的端口给外部应用，这样就可以 同时多个应用访问nacos端口，而且可以使用相同的nacos配置。docker的特性，只要底层的镜像image还在，就可以在其上创建多个容器使用。</strong>-p 10002:8848   本机端口：docker中暴露的端口，也就是将docker中的8848端口暴露给外面的10002端口</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run --name nacos-server2 --env <span class="hljs-built_in">MODE</span>=standalone -v D:\docker\Workspace_docker\nacos\conf\application.properties:\home\nacos\conf\application.properties -d -p <span class="hljs-number">10002</span>:<span class="hljs-number">8848</span> nacos/nacos-server:<span class="hljs-number">1</span>.<span class="hljs-number">4</span>.<span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>



<h3 id="4-docker安装redis"><a href="#4-docker安装redis" class="headerlink" title="4. docker安装redis"></a>4. docker安装redis</h3><p>docker run –restart=always –log-opt max-size=100m –log-opt max-file=2 -p 6379:6379 –name myredis -v D:\docker\Workspace_docker\redis\myredis.conf:\etc\redis\redis.conf -v D:\docker\Workspace_docker\redis\data:\data -d redis redis-server \etc\redis\redis.conf  –appendonly yes  –requirepass 000415</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker run -d -p 6379:6379 -v D:/docker/Workspace_docker/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf -v D:/docker/Workspace_docker/redis/data:/data --name docker-redis -d redis redis-server/etc/redis/redis.conf  --appendonly <span class="hljs-built_in">yes</span>  <br></code></pre></td></tr></table></figure>

<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run -itd --name redis-test -p <span class="hljs-number">6379</span>:<span class="hljs-number">6379</span> redis<br></code></pre></td></tr></table></figure>







<h3 id="5-docker使用技巧"><a href="#5-docker使用技巧" class="headerlink" title="5. docker使用技巧"></a>5. docker使用技巧</h3><p>删除容器三步走：</p>
<p>1.查询容器id</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker ps<br></code></pre></td></tr></table></figure>

<p>2.停止运行的容器</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker stop <span class="hljs-number">9299317</span>bdedf<br></code></pre></td></tr></table></figure>

<p>3.删除容器</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker rm <span class="hljs-number">9299317</span>bdedf<br></code></pre></td></tr></table></figure>

<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230209160024238.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230209160024238"></p>
<p>4.进入容器</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker exec -it nacostest bash<br></code></pre></td></tr></table></figure>



<h3 id="6-给容器固定ip"><a href="#6-给容器固定ip" class="headerlink" title="6. 给容器固定ip"></a>6. 给容器固定ip</h3><p>由于工作需要，需要给Docker设置固定IP，在网上的查找了一些资料，自己试着实践一下，留个笔记。</p>
<p>经常用Docker模拟项目在生产环境中的部署，往往需要同时开好几台Docker容器，而且有时安装的软件需要绑定Docker局域网中的其他容器，如 MongoDB 副本集部署的时候，就需要绑定其他容器的内网IP。</p>
<p>但是，Docker 每次重启后，容器的IP地址会变化，查询了资料，Docker是支持设置固定IP的。</p>
<p>Docker 默认网络</p>
<p>Docker安装后，默认会创建下面三种网络类型：</p>
<figure class="highlight basic"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs basic"><span class="hljs-symbol">1 </span>$ docker network ls<br><span class="hljs-symbol">2 </span>NETWORK ID     <span class="hljs-keyword">NAME</span> DRIVERSCOPE<br><span class="hljs-symbol">3 </span><span class="hljs-number">9781</span>b1f585ae    bridgebridgelocal<br><span class="hljs-symbol">4 </span><span class="hljs-number">1252</span>da701e55    host host local<br><span class="hljs-symbol">5 </span><span class="hljs-number">237</span>ea3d5cfbf    none null local<br></code></pre></td></tr></table></figure>

<p>启动 Docker的时候，用 –network 参数，可以指定网络类型，如：</p>
<figure class="highlight basic"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs basic"><span class="hljs-symbol">1 </span>~ docker <span class="hljs-keyword">run</span> -itd --<span class="hljs-keyword">name</span> test1 --network bridge --ip <span class="hljs-number">172.17</span>.<span class="hljs-number">0.10</span> centos:latest /bin/bash<br></code></pre></td></tr></table></figure>


<p>bridge：桥接网络</p>
<p>默认情况下启动的Docker容器，都是使用 bridge，Docker安装时创建的桥接网络，每次Docker容器重启时，会按照顺序获取对应的IP地址，这个就导致重启下，Docker的IP地址就变了</p>
<p>none：无指定网络</p>
<p>使用 –network=none ，docker 容器就不会分配局域网的IP</p>
<p>host： 主机网络</p>
<p>使用 –network=host，此时，Docker 容器的网络会附属在主机上，两者是互通的。</p>
<p>例如，在容器中运行一个Web服务，监听8080端口，则主机的8080端口就会自动映射到容器中。</p>
<p>创建自定义网络：（设置固定IP）</p>
<p>启动Docker容器的时候，使用默认的网络是不支持指派固定IP的，如下：</p>
<figure class="highlight basic"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs basic"><span class="hljs-symbol">1 </span>~ docker <span class="hljs-keyword">run</span> -itd --net bridge --ip <span class="hljs-number">172.17</span>.<span class="hljs-number">0.10</span> centos:latest /bin/bash<br><span class="hljs-symbol">2 </span><span class="hljs-number">6</span>eb1f228cf308d1c60db30093c126acbfd0cb21d76cb448c678bab0f1a7c0df6<br><span class="hljs-symbol">3 </span>docker: <span class="hljs-keyword">Error</span> response from daemon: User specified IP address is supported <span class="hljs-keyword">on</span> user defined networks only.<br></code></pre></td></tr></table></figure>

<p>因此，需要创建自定义网络，下面是具体的步骤：</p>
<p>步骤1: 创建自定义网络</p>
<p>创建自定义网络，并且指定网段：172.18.0.0/16</p>
<figure class="highlight basic"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs basic"><span class="hljs-symbol">1 </span>➜ ~ docker network create --subnet=<span class="hljs-number">172.18</span>.<span class="hljs-number">0.0</span>/<span class="hljs-number">16</span> mynetwork<br><span class="hljs-symbol">2 </span>➜ ~ docker network ls<br><span class="hljs-symbol">3 </span>NETWORK ID     <span class="hljs-keyword">NAME</span> DRIVERSCOPE<br><span class="hljs-symbol">4 </span><span class="hljs-number">9781</span>b1f585ae    bridgebridgelocal<br><span class="hljs-symbol">5 </span><span class="hljs-number">1252</span>da701e55    host host local<br><span class="hljs-symbol">6 </span><span class="hljs-number">4</span>f11ae9c85de    mynetwork      bridgelocal<br><span class="hljs-symbol">7 </span><span class="hljs-number">237</span>ea3d5cfbf    none null local<br></code></pre></td></tr></table></figure>

<p>步骤2: 创建Docker容器</p>
<p>复制代码 代码如下:</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run -itd --name networkTest1 --<span class="hljs-built_in">net</span> mynetwork --ip <span class="hljs-number">172</span>.<span class="hljs-number">18</span>.<span class="hljs-number">0</span>.<span class="hljs-number">2</span> centos:latest /bin/bash<br></code></pre></td></tr></table></figure>

<p>指定mysql容器的固定ip</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker run -p <span class="hljs-number">3306</span>:<span class="hljs-number">3306</span> --name ruoyi-mysql --<span class="hljs-built_in">net</span> mynetwork --ip <span class="hljs-number">172</span>.<span class="hljs-number">18</span>.<span class="hljs-number">0</span>.<span class="hljs-number">2</span> -e MYSQL_ROOT_PASSWORD=Nari_jysp3.<span class="hljs-number">0</span> -d mysql:<span class="hljs-number">5</span>.<span class="hljs-number">7</span><br></code></pre></td></tr></table></figure>

<p>查看ip</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker inspect ruoyi-mysql<br></code></pre></td></tr></table></figure>

<p>这个时候，创建的Docker容器就会持有 172.18.0.2 这个IP.</p>
<h3 id="7-连接不同网段的容器"><a href="#7-连接不同网段的容器" class="headerlink" title="7. 连接不同网段的容器"></a>7. 连接不同网段的容器</h3><p>正常情况下两个不同网段之间的容器不能互相ping通，如下图，网段为172.17的容器和网段为192.168的容器之间不能互相ping通，</p>
<p>查看容器ip</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker exec -it centos1 ifconfig<br></code></pre></td></tr></table></figure>

<p>两个容器ping</p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">docker</span> exec -it centos-mynet-<span class="hljs-number">01</span> ping centos01<br></code></pre></td></tr></table></figure>



<p>2、使用以下命令连通不在同一个网段下的[Docker容器]</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs cmd">docker network connect 网络名称 容器名称<br><br>docker network connect mynetwork nacos-server-<span class="hljs-number">2</span>.<span class="hljs-number">1</span>.<span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>

<p>3、再次通过容器名称来互相ping，发现能互相ping通</p>
<p>4、使用以下命令查看名为mynet的网络的元数据，发现名为mynet的自定义网络下多出一个名为centos01的容器，也就是说把centos01容器放到了mynet网络下，所以不同网段下能互相连通</p>
<figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ebnf"><span class="hljs-attribute">docker network inspect mynetwork</span><br></code></pre></td></tr></table></figure>

<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230413113954453.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230413113954453"></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/hello_lixi.gitee.io/categories/java%E5%BC%80%E5%8F%91-docker/" class="category-chain-item">java开发,docker</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/hello_lixi.gitee.io/tags/docker/">#docker</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Docker容器的安装使用</div>
      <div>https://xistudycode.gitee.io/hello_lixi.gitee.io/2023/05/11/docker-容器的安装使用/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Mr.LI</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年5月11日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/hello_lixi.gitee.io/2023/05/11/Vue+Springboot%E5%A4%9A%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/" title="Vue2+Springboot 实现多文件上传">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Vue2+Springboot 实现多文件上传</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/hello_lixi.gitee.io/2023/05/11/Spring%E5%BC%80%E5%8F%91%E4%B8%AD%E9%81%87%E5%88%B0%E7%9A%84redis%E9%97%AE%E9%A2%98/" title="SpringBoot开发中遇到的redis问题">
                        <span class="hidden-mobile">SpringBoot开发中遇到的redis问题</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>xBolg</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>@Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/hello_lixi.gitee.io/js/events.js" ></script>
<script  src="/hello_lixi.gitee.io/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/hello_lixi.gitee.io/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/hello_lixi.gitee.io/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/hello_lixi.gitee.io/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
